package com.desmart.renjiao.main;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.desmart.renjiao.R;
import com.lemon.library.common.ARoutePath;
import com.lemon.library.common.CommonActivity;
import com.lemon.library.utils.StatusBarUtil;
import com.lemon.library.view.TipRadioButton;

@Route(path = ARoutePath.Main.MainActivity)
public class MainActivity extends CommonActivity<MainM,MainP> implements RadioGroup.OnCheckedChangeListener,Main.MainV {

    private Fragment selectFragment;
    private TipRadioButton rbHome;
    private TipRadioButton rbWorkplace;
    private TipRadioButton rbAddressList;
    private TipRadioButton rbMine;
    private RadioGroup rg_main_bottom;
    private TextView tvMainTitle;
    private Fragment home_fragment;
    private Fragment workpalce_framgent;
    private Fragment addresslist_fragment;
    private Fragment mine_fragment;
    private LinearLayout llTitleCommon;
    private ImageView ivSetting;
    private View titleView;

    @Override
    public int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView(Activity activity) {
        StatusBarUtil.setTransparent(this);
        swipeBackLayout.setEnableGesture(false);
        llTitleCommon = activity.findViewById(R.id.common_title_ll);
        tvMainTitle = activity.findViewById(R.id.main_title_tv);
        rbHome = activity.findViewById(R.id.rb_home);
        rbWorkplace = activity.findViewById(R.id.rb_workplace);
        rbAddressList = activity.findViewById(R.id.rb_addresslist);
        rbMine = activity.findViewById(R.id.rb_mine);
        rg_main_bottom = activity.findViewById(R.id.rg_main_bottom);
        rg_main_bottom.setOnCheckedChangeListener(this);
        ivSetting = activity.findViewById(R.id.iv_setting);
        ivSetting.setOnClickListener(this);
        titleView = activity.findViewById(R.id.common_title_rl);
        setTopView();
        bActive = true;
        titleView.setVisibility(View.GONE);
        setFirstFragment();
    }

    private void setTopView() {
        int statusBarHeight = StatusBarUtil.getStatusBarHeight(this);
        titleView.setPadding(0,statusBarHeight,0,0);
    }

    @Override
    public void initData(Context context) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.tokenPort();
    }

    public void setFirstFragment() {
        Fragment fragmentByIndex = getFragmentByIndex(1);
        if (fragmentByIndex != null) {
            addFragment(fragmentByIndex);
        }
    }

    public void addFragment(Fragment fragment) {
        try {
            FragmentManager fragmentManager = getSupportFragmentManager();
            if (!fragment.isAdded()) {
                fragmentManager.beginTransaction().add(R.id.centerlayout, fragment).commitAllowingStateLoss();
                selectFragment = fragment;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void switchFragment(Fragment to) {
        try {
            if (!bActive || to == selectFragment) {
                return;
            }
            FragmentManager fragmentManager = getSupportFragmentManager();
            if (to.isAdded() || fragmentManager.getFragments().contains(to)) {
                // 隐藏当前的fragment，显示下一个
                fragmentManager.beginTransaction().hide(selectFragment).show(to).commitAllowingStateLoss();
            } else {
                // 隐藏当前的fragment，add下一个到Activity中
                fragmentManager.beginTransaction().hide(selectFragment).add(R.id.centerlayout, to).commitAllowingStateLoss();
            }
            selectFragment = to;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        ivSetting.setImageResource(R.drawable.icon_setting_white);
        llTitleCommon.setVisibility(View.VISIBLE);
        titleView.setVisibility(View.VISIBLE);
        switch (checkedId) {
            case R.id.rb_home:
                tvMainTitle.setText(getString(R.string.main_home));
                switchFragment(getFragmentByIndex(1));
                llTitleCommon.setVisibility(View.GONE);
                titleView.setVisibility(View.GONE);
                break;
            case R.id.rb_workplace:
                tvMainTitle.setText(getString(R.string.main_workplace));
                switchFragment(getFragmentByIndex(2));
                break;
            case R.id.rb_addresslist:
                tvMainTitle.setText(getString(R.string.main_address_book));
                ivSetting.setImageResource(R.drawable.lib_ic_search);
                switchFragment(getFragmentByIndex(3));
                break;
            case R.id.rb_mine:
                tvMainTitle.setText(getString(R.string.main_mine));
                switchFragment(getFragmentByIndex(4));
                break;
            default:
                break;
        }

    }

    public Fragment getFragmentByIndex(int index) {
        if (index == 1) {
            if (home_fragment == null)
                home_fragment = (Fragment) ARouter.getInstance().build(ARoutePath.Main.HomeFragment).navigation();
            return home_fragment;
        } else if (index == 2) {
            if (workpalce_framgent == null)
                workpalce_framgent = (Fragment) ARouter.getInstance().build(ARoutePath.Main.WorkPlaceFragment).navigation();
            return workpalce_framgent;
        } else if (index == 3) {
            if (addresslist_fragment == null)
                addresslist_fragment = (Fragment) ARouter.getInstance().build(ARoutePath.Main.AddresslistFragment).navigation();
            return addresslist_fragment;
        }else if (index == 4) {
            if (mine_fragment == null)
                mine_fragment = (Fragment) ARouter.getInstance().build(ARoutePath.Main.MineFragment).navigation();
            return mine_fragment;
        }
        return null;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.iv_setting){
            if (selectFragment == addresslist_fragment){
                ARouter.getInstance().build(ARoutePath.addresslist.MemberSearchActivity).navigation();
            }
        }
    }
}
